package com.eleventwelve.iris.persistence.evolve;

import java.util.ArrayList;
import java.util.List;

import org.opentides.bean.SystemCodes;
import org.opentides.persistence.SystemCodesDAO;
import org.opentides.persistence.evolve.DBEvolve;
import org.opentides.persistence.evolve.Evolver;

import com.eleventwelve.iris.dao.AllergenDao;
import com.eleventwelve.iris.dao.DiseaseDao;
import com.eleventwelve.iris.entity.Allergen;
import com.eleventwelve.iris.entity.Disease;

/**
 * 
 * @author Ashley
 *
 */
public class DBEvolve001 extends Evolver implements DBEvolve {

	private SystemCodesDAO systemCodesDAO;
	
	private AllergenDao allergenDao;
	
	private DiseaseDao diseaseDao;
	
	@Override
	public void execute() {
		importDiseaseAndAllergen();
		List<SystemCodes> systemCodes = new ArrayList<SystemCodes>();
		systemCodes.addAll(prepareGenders());
		systemCodes.addAll(prepareContactNumberTypes());	
		systemCodes.addAll(prepareAllergyStatus());
		systemCodes.addAll(prepareDiseaseStatus());	
		systemCodesDAO.saveAllEntityModel(systemCodes);
	}
	
	private List<SystemCodes> prepareGenders(){
		List<SystemCodes> systemCodes = new ArrayList<SystemCodes>();
		systemCodes.add(new SystemCodes("GENDER", "MALE", "Male"));
		systemCodes.add(new SystemCodes("GENDER", "FEMALE", "Female"));
		return systemCodes;
	}
	
	private List<SystemCodes> prepareContactNumberTypes(){
		List<SystemCodes> systemCodes = new ArrayList<SystemCodes>();
		systemCodes.add(new SystemCodes("NUMBER_TYPE", "MOBILE", "Mobile"));
		systemCodes.add(new SystemCodes("NUMBER_TYPE", "LANDLINE", "Landline"));
		return systemCodes;
	}
	
	private List<SystemCodes> prepareAllergyStatus(){
		List<SystemCodes> systemCodes = new ArrayList<SystemCodes>();
		systemCodes.add(new SystemCodes("ALLERGY_STATUS", "ACTIVE", "Active"));
		systemCodes.add(new SystemCodes("ALLERGY_STATUS", "INACTIVE", "Inactive"));
		return systemCodes;
	}
	
	private List<SystemCodes> prepareDiseaseStatus(){
		List<SystemCodes> systemCodes = new ArrayList<SystemCodes>();
		systemCodes.add(new SystemCodes("DISEASE_STATUS", "PRESENT", "Present"));
		systemCodes.add(new SystemCodes("DISEASE_STATUS", "PAST", "Past"));
		return systemCodes;
	}
	
	public void importDiseaseAndAllergen() {
		String[] allergens = {
				"Food",
				"Aspirin",
				"Latex",
				"Lidocaine",
				"Hydrocortisone",
				"Hydroquinone or Skin Bleaching Agents"};
		
		for (String name : allergens) {
			Allergen allergen = new Allergen();
			allergen.setName(name);
			allergenDao.saveEntityModel(allergen);
		}
		
		String[] diseases = {
				"Cancer",
				"Diabetes",
				"Arthritis",
				"Thyroid Imbalance",
				"HIV/AIDS",
				"High Blood Pressure",
				"Seizure Disorder",
				"Blood Clotting Abnormalities",
				"Keloid Scarring",
				"Herpes",
				"Hepatitis",
				"Cardiac Problems/Anomalies",
				"Hormone Imbalance",
				"Kidney/Renal Problems",
				"Asthma",
				"Reproductive tract conditions/diseases or dysfunctions",
				"Any active infection"};
		
		for (String name : diseases) {
			Disease disease = new Disease();
			disease.setName(name);
			diseaseDao.saveEntityModel(disease);
		}
	}
	
	@Override
	public String getDescription() {
		return new String("DBEvolve to add initial set of system codes: gender, contact number type, allergy status, disease status, prescription result status");
	}

	@Override
	public int getVersion() {
		return 1;
	}
	
	public SystemCodesDAO getSystemCodesDAO() {
		return systemCodesDAO;
	}
	
	public void setSystemCodesDAO(SystemCodesDAO systemCodesDAO) {
		this.systemCodesDAO = systemCodesDAO;
	}

	public void setAllergenDao(AllergenDao allergenDao) {
		this.allergenDao = allergenDao;
	}

	public DiseaseDao getDiseaseDao() {
		return diseaseDao;
	}

	public void setDiseaseDao(DiseaseDao diseaseDao) {
		this.diseaseDao = diseaseDao;
	}

}
